package tzjz.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * All rights Reserved, Designed By www.pousheng.com
 *
 * @version V1.0
 * @Title: RedisConfig
 * @Package com.lsy.redisdemo.config
 * @Description: 简单描述下这个类是做什么用的
 * */
@Configuration
public class RedisConfig {
    /**
     * @name: redisTemplate
     * @description: RedisTemplate
     * @param redisConnectionFactory
     * @return: org.springframework.data.redis.core.RedisTemplate<java.lang.String,java.lang.Object>
     * @date: 2020-12-04 13:19
     *
    */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer(Object.class);
        // 设置键（key）的序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置value序列化
        redisTemplate.setValueSerializer(serializer);
        // 设置HashKey序列化 为啥要hashkey
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // 设置HashValue序列化
        redisTemplate.setHashValueSerializer(serializer);
        // 默认序列化
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}